package com.example.shiwu;


import static com.example.shiwu.util.FileDownUtil.fileOver;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.shiwu.service.UserService;
import com.example.shiwu.util.NetworkUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class LoginActivity extends AppCompatActivity {

    private Button mBtnLogin;
    private TextView mEtName;
    private TextView mEtPassword;
    private UserService userService;
    private Context myContent;
    public String mEtNameValue,mEtPasswordValue;
    private CheckBox remember;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        myContent = this;
        userService = new UserService();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        mBtnLogin = findViewById(R.id.btn_login);
        mEtName = findViewById(R.id.et_name);
        mEtPassword = findViewById(R.id.et_password);
        remember = findViewById(R.id.remember);
        //readDownloadFile();
        //fileOver();
        boolean isRemember = preferences.getBoolean("remember_password",false);
        if (isRemember){
            String Name = preferences.getString("Name","");
            String Password = preferences.getString("Password","");
            mEtName.setText(Name);
            mEtPassword.setText(Password);
            remember.setChecked(true);
        }

        mBtnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
//                if (!NetworkUtils.isNetworkAvailable(myContent)) {
//                    Toast.makeText(myContent, "网络未连接！", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                mEtNameValue = mEtName.getText().toString();
                mEtPasswordValue = mEtPassword.getText().toString();

                try {
                    if(mEtNameValue.length()>0 && mEtPasswordValue.length()>0){
                        boolean bl = userService.login(mEtName.getText().toString(),mEtPassword.getText().toString(),0,myContent);//默认注册swAdmin密码swAdmin123,user密码123456
                        if(bl){
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                            editor = preferences.edit();
                            if(remember.isChecked()){
                                editor.putBoolean("remember_password",true);
                                editor.putString("Name",mEtNameValue);
                                editor.putString("Password",mEtPasswordValue);
                            }else{editor.clear();}
                            editor.apply();
                        }
                    }else{
                        Toast.makeText(myContent, "用户名、密码不能为空！", Toast.LENGTH_SHORT).show();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1;


    private void readDownloadFile() {
        ContentResolver contentResolver = getContentResolver();
        String selection = MediaStore.Files.FileColumns.DISPLAY_NAME + " =? AND " +
                MediaStore.Files.FileColumns.RELATIVE_PATH + " LIKE '%Download%'";
        String[] selectionArgs = {"ABMN.txt"};
        String[] projection = {MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA};

        Cursor cursor = contentResolver.query(MediaStore.Files.getContentUri("external"),
                projection, selection, selectionArgs, null);

        if (cursor != null && cursor.moveToFirst()) {
            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            String filePath = cursor.getString(dataColumnIndex);
            try {
                File file = new File(filePath);
                FileInputStream fis = new FileInputStream(file);
                // 在这里进行文件读取操作
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            cursor.close();
        }
    }
}
